package com.company.pangu.aliyun.live.generator;

import com.company.pangu.aliyun.live.enums.PushProtocolType;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.text.MessageFormat;

/**
 * <p>推流地址构造器</p >
 * https://help.aliyun.com/document_detail/199339.html
 *
 * @author youyoukele
 * @version 1.0
 * @date 2021/10/28
 */
@Builder
@Data
public class PushUrlGenerator implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * rtmp://DomainName/AppName/StreamName?auth_key=timestamp-rand-uid-md5hash
     */
    private static final String FORMATTER = "{0}{1}/{2}/{3}?auth_key={4}";

    /**
     * 推流协议，目前只支持 RTMP
     */
    private PushProtocolType pushProtocolType;

    /**
     * 推流域名
     */
    private String pushDomain;

    /**
     * 应用名
     */
    private String appName;

    /**
     * 直播流
     */
    private String streamName;

    /**
     * 鉴权key
     * {@link AuthKeyGenerator}
     */
    private String authKey;

    /**
     * 生成直播流地址
     *
     * @return
     */
    public String gen() {
        return MessageFormat.format(PushUrlGenerator.FORMATTER, pushProtocolType.getNetProtocol(), pushDomain, appName, streamName, authKey);
    }

    public static void main(String[] args) {
        PushUrlGenerator pushUrlGenerator = PushUrlGenerator.builder()
                .appName("app")
                .streamName("stream")
                .pushProtocolType(PushProtocolType.RTMP)
                .pushDomain("push.abcd.vip")
                .authKey("authKey")
                .build();
        System.out.println(pushUrlGenerator.gen());
    }

}
